package edu.ustb.efws.base.service;

import edu.ustb.efws.BaseException;
import edu.ustb.efws.base.domain.User;
import edu.ustb.efws.base.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户业务服务
 *
 * @author TS Group
 */
@Service
@Transactional(readOnly = true)
public class UserService {
    private final UserMapper mapper;

    @Autowired
    public UserService(UserMapper mapper) {
        this.mapper = mapper;
    }

    /**
     * 新增用户
     *
     * @param t {@link User}
     * @return {@link User}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public User save(User t){

        if(mapper.usernameCount(t.getUsername()) > 0){
            throw new BaseException("用户已经存在");
        }

        t.setCreateTime(new Date());
        t.setUpdateTime(new Date());
        t.setDelete(false);
        mapper.insert(t);

        return t;
    }

    /**
     * 修改用户
     *
     * @param t {@link User}
     * @return {@link User}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public User update(User t){
        if(mapper.update(t) == 0){
            throw new BaseException("修改用户失败");
        }
        return mapper.findOne(t.getId());
    }

    /**
     * 删除用户
     *
     * @param id 用户编号
     * @return true:删除成功
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delete(long id){
        User t = mapper.findOne(id);
        if(t == null){
            throw new BaseException("用户不存在");
        }

        String username = t.getUsername() + "@" + System.currentTimeMillis();
        return mapper.delete(id, username) > 0;
    }

    /**
     * 修改密码
     *
     * @param id 用编号
     * @param password 密码
     * @param newPassword 新密码
     * @return true 修改成功
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updatePassword(long id, String password, String newPassword){
        User t = get(id);
        if(!StringUtils.equals(t.getPassword(), password)){
            throw new BaseException("用户密码错误");
        }
        return mapper.updatePassword(id, newPassword) > 0;
    }

    /**
     * 重置密码
     *
     * @param id 编号
     * @param newPassword 新密码
     * @return true 重置成功
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean resetPassword(long id, String newPassword){
        return mapper.updatePassword(id, newPassword) > 0;
    }

    /**
     * 获取用户
     *
     * @param id 用户编号
     * @return {@link User}
     */
    public User get(long id){
        User t = mapper.findOne(id);
        if(t == null){
            throw new BaseException("用户不存在");
        }
        return t;
    }

    /**
     * 获取并验证用户
     *
     * @param username 用户名
     * @param password 用户密码
     * @return {@link User}
     */
    public User getAndValidate(String username, String password) {
        User t = mapper.findOneByUsername(username);
        if(t == null || !StringUtils.equals(t.getPassword(), password)){
            throw new BaseException(101, "用户名或密码错误");
        }
        return t;
    }

    /**
     * 查询接收报警短信用户
     *
     * @return 用户集合
     */
    public List<User> querySms(){
        return mapper.findSms();
    }

    /**
     * 查询记用户录数
     *
     * @param username 用户名
     * @param name 姓名
     * @return 记录数
     */
    public long count(String username, String name){
        return mapper.count(username, name);
    }

    /**
     * 查询用户
     *
     * @param username 用户名
     * @param name 姓名
     * @param offset 偏移量
     * @param limit 查询记录数
     * @return 查询用户集合
     */
    public List<User> query(String username, String name, int offset, int limit){
        return mapper.find(username, name, offset, limit);
    }
}
